package com.teacold.customdemo.chapter_five;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.view.View;
//scalable vector graphics Illustrator

public class AlipayView extends View {
    private Path mPath,mCirclePath;
    private Paint mPaint;
    private PathMeasure mPathMeasure;
    private Float mCurren;
    private int mCentX=100,mCentY=100,mRadius=50;


    public AlipayView(Context context) {
        super(context);
        setLayerType(LAYER_TYPE_SOFTWARE,null);
        mPaint=new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        mPath=new Path();
        mCirclePath=new Path();
        mCirclePath.addCircle(mCentX,mCentY,mRadius,Path.Direction.CW);
        mCirclePath.moveTo(mCentX-mRadius/2,mCentY);
        mCirclePath.lineTo(mCentX,mCentY+mRadius/2);
        mCirclePath.lineTo(mCentX+mRadius/2,mCentY-mRadius/3);
        mPathMeasure=new PathMeasure(mCirclePath,false);

        ValueAnimator valueAnimator=ValueAnimator.ofFloat(0f,2f);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mCurren= (Float) animation.getAnimatedValue();
                invalidate();
            }
        });
        valueAnimator.setDuration(3000);
        valueAnimator.start();
    }

    public AlipayView(Context context, AttributeSet attrs) {
        this(context);
    }

    public AlipayView(Context context,  AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        if(mCurren<1){
            float stop=mPathMeasure.getLength()*mCurren;
            mPathMeasure.getSegment(0,stop,mPath,true);
        }else if(mCurren==1){
            mPathMeasure.getSegment(0,mPathMeasure.getLength(),mPath,true);
            mPathMeasure.nextContour();
        }else {
            float stop =mPathMeasure.getLength()*(mCurren-1);
            mPathMeasure.getSegment(0,stop,mPath,true);
        }
        canvas.drawPath(mPath,mPaint);
    }
}
